import * as THREE from '../../../../modules/three/three.module.js'
import { flatten } from '../util/StaticMethods.js';

export default class FacingIndicator extends THREE.Group {
  constructor(radius = 0.05, height = 0.33) {
    super();
    // const radius = 0.1;
    // const height = 0.33;
    const origin = new THREE.Vector3(0, 0, 0);
    const pts = [origin];
    const faceIndices = [];
    const lineIndices = [];
    const segment = 6;
    const angle = (Math.PI * 2) / segment;
    for (let i = 0; i < segment; i++) {
      const a = angle * i;
      const x = radius * Math.sin(a);
      const y = radius * Math.cos(a);
      const pt = new THREE.Vector3(x, y, 0);
      pts.push(pt);
      if (i === segment - 1) {
        lineIndices.push(1, segment);
        faceIndices.push(0, 1, segment);
      } else {
        lineIndices.push(i + 1, i + 2);
        faceIndices.push(0, i + 2, i + 1);
      }
    }

    pts.push(new THREE.Vector3(0, 0, height));
    lineIndices.push(0, segment + 1);

    const lineGeo = new THREE.BufferGeometry();
    lineGeo.setIndex(lineIndices);
    const posAttr = new THREE.BufferAttribute(flatten(pts), 3)
    lineGeo.setAttribute('position', posAttr);
    const lineSegments = new THREE.LineSegments(
      lineGeo,
      new THREE.LineBasicMaterial({ color: '#ffbc11', toneMapped: false }),
    );
    this.add(lineSegments);

    // const meshGeo = new THREE.BufferGeometry()
    // meshGeo.setIndex(faceIndices)
    // meshGeo.setAttribute('position', posAttr)
    // const mesh = new THREE.Mesh(meshGeo, new THREE.MeshBasicMaterial({color: '#009d32', opacity: 0.6, transparent: true})) //
    // this.add(mesh)
  }
}
